﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict.DataModel;
using Dict.Blo;

namespace Room
{
    public class RCAIGate : RCAIUnionFightBase
    {
        #region 数据
        InstUFGate instData;
        public TableUnionFightGate dictData;
        #endregion

        //public static int NormalIndex = 0;
        //public static int DestroyIndex = 1;
        //public static int BeHitIndex = 2;
        //public static int DestroyMeshIndex = 3;


        //public Transform[] effectObjs;


        public List<float> hpState = new List<float>();
        public List<GameObject> states = new List<GameObject>();

        public RCAIGate(RoomCharactor owner)
            :base(owner)
        {
            this.type = AIType.UnionGate;
        }

        public void Init(InstUFGate instData)
        {
            UpdateData(instData);
            InitFSM();
        }

        public void UpdateData(InstUFGate instData)
        {
            this.instData = instData;
            this.dictData = DictUnionFightGateBlo.GetUnionFightGateByID(instData.id + "");
            if (dictData != null)
            {
                var url = dictData.GateModel;
                if (sceneObj == null)
                {
                    FindSceneObj(url);
                }
            }

            if (states.Count <= 0)
            {
                if (DictUnionFightGateBlo.GetEffectNodePaths(instData.id + "") != null)
                {
                    foreach (var path in DictUnionFightGateBlo.GetEffectNodePaths(instData.id + ""))
                    {
                        states.Add(GameObject.Find(url + "/" + path));
                    }
                }
            }

            if(hpState.Count <=0)
            {
                var hpStr = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.UnionFightGateState;
                var sss = hpStr.Trim().Split(new char[1] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);
                foreach(var s in sss)
                {
                    var v = float.Parse(s);
                    hpState.Add(v);
                }
            }

            UpdateDisplayState();
            this.SetCamp(instData.camp);
        }


        #region FSM
        private UnionGateState currState = UnionGateState.None;
        private Dictionary<UnionGateState, RCAIGateBase> allState = new Dictionary<UnionGateState, RCAIGateBase>();
        public RCAIGateBase currentState;
        public override void Tick()
        {
            base.Tick();
            
            if(currentState!=null)
            {
                currentState.Tick();
            }
        }

        private void InitFSM()
        {
            allState[UnionGateState.None] = new RCAIGateBase();
            allState[UnionGateState.Normal] = new RCAIGateNormal();
            allState[UnionGateState.Destroy] = new RCAIGateDestroy();

            foreach (var state in allState)
            {
                state.Value.controller = this;
            }
            SwitchState((int)UnionGateState.Normal);
        }

        public override void SwitchState(int newState)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionGateState)newState;
                allState[currState].Enter();
                currentState = allState[currState];
            }
        }

        public override void SwitchState(int newState, object param)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionGateState)newState;
                allState[currState].Enter(param);
                currentState = allState[currState];
            }
        }

        public override void TickHp(RoomCharactor src, float hpDelta)
        {
            if(hpDelta < 0)
            {
                BeHit(src);
            }

            instData.hp += hpDelta;
            if(instData.hp < 0)
            {
                instData.hp = 0;
                Debug.Log("[GATE] charactor is die " + owner.name);
            }

            UpdateDisplayState();
        }

        public override void SetHp(float hp)
        {
            //base.SetHp(hp);
        }

        #endregion

        protected override void BeHit(RoomCharactor srcOwner)
        {
            //CreateBloodBar();
            CreateEffect();
        }
        

        void CreateEffect()
        {
            Spawn(RoomUnionFight.GATE_HIT,1f);
            //obj.transform.SetParent(effectObjs[BeHitIndex].parent);
            //obj.transform.localPosition = effectObjs[BeHitIndex].localPosition;
        }

        void UpdateDisplayState()
        {
            if (states.Count > 0)
            {
                foreach (var obj in states)
                {
                    obj.SetActive(false);
                }
                var currHp = instData.hp / instData.maxHp;
                int index = 0;
                foreach (var dictHp in hpState)
                {
                    if (currHp <= dictHp)
                    {
                        index++;
                    }
                }
                index--;
                states[index].SetActive(true);
            }
        }

        //void CreateNavObstacle()
        //{
        //    var nav0 = owner.gameObject.GetComponent<NavMeshObstacle>();
        //    if(nav0==null)
        //    {
        //        nav0 = owner.gameObject.AddComponent<NavMeshObstacle>();
        //    }
        //    nav0.size = new Vector3(6, 6, 2f);
        //    nav0.shape = NavMeshObstacleShape.Box;
        //    nav0.carving = true;
        //}

        //public void SetEnableObstacle(bool enable)
        //{
        //    if (enable)
        //    {
        //        var nav = owner.gameObject.GetComponent<NavMeshObstacle>();
        //        if (nav != null)
        //        {
        //            nav.enabled = true;
        //        }
        //        else
        //        {
        //            CreateNavObstacle();
        //        }
        //    }
        //    else
        //    {
        //        var nav = owner.gameObject.GetComponent<NavMeshObstacle>();
        //        if (nav != null)
        //        {
        //            nav.enabled = false;
        //        }
        //    }
        //}
    }
}
